package cs236369.webserver;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.HashMap;
import java.util.Map;



import org.apache.http.params.BasicHttpParams;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import cs236369.webserver.WebServer.RequestHandler;

public class RequestHandlerTest {
	
	private Request tstRequest;
	private WebServer.RequestHandler tstRequestHandler;
	private	Socket socketClient;
	private	Socket socketServer;
	private ServerSocket serverSocket;
	private Thread listeningThread;
	private WebServer testServer;
	
	@Before
	public void init() throws Exception {
		FileInputStream fip = new FileInputStream("configTst.xml");
		testServer = new WebServer(fip);
		serverSocket = new ServerSocket(9999);
		Listner listner = new Listner();
		listeningThread = new Thread(listner); 
		listeningThread.start();
		socketClient = new  Socket(InetAddress.getLocalHost().getHostAddress(), 9999);
	}
	
	class Listner implements Runnable{

		@Override
		public void run() {
			try {
				socketServer = serverSocket.accept();
			} catch (IOException e) {
				e.printStackTrace();
				throw new RuntimeException();
			}
		}
		
	}

	@After
	public void destroy() throws IOException, InterruptedException{
		socketClient.close();
		socketServer.close();
		listeningThread.join();
		serverSocket.close();
	}
	
	@Test
	public void testBasicRun() throws IOException {
		//test for basic response, asking for none existing file
		Map<String,String> httpParams = new HashMap<String, String>();
		httpParams.put("host", "127.0.0.1");
		PrintStream out = new PrintStream(socketClient.getOutputStream());
		out.write(0);
		out.flush();
		
		System.out.println("blablabla");
		while (socketServer == null);
		System.out.println("blabla");
		tstRequest = new Request(socketServer, "/tst.tsp", "POST", httpParams, "bla=a&blabla=b");
		
		tstRequestHandler = testServer.new RequestHandler(tstRequest);
		tstRequestHandler.run();
		System.out.println("Here!");

		//reading handled page
	    InputStreamReader in = new InputStreamReader(socketClient.getInputStream());
	      int c;
	      try
	      {
	      while ((c = in.read())!=-1)
	        System.out.print((char)c);
	      }catch (SocketException e){
	    	  if (!e.getMessage().equals("Connection reset"))
	    	  {
	    		  throw e;
	    	  }
	      }
	      
	}
	
	@Test
	public void testBasicRun1() throws IOException {
		//test for basic response, asking for folder
		Map<String,String> httpParams = new HashMap<String, String>();
		httpParams.put("host", "127.0.0.1");
		PrintStream out = new PrintStream(socketClient.getOutputStream());
		out.write(0);
		out.flush();
		while (socketServer == null);
		tstRequest = new Request(socketServer, "/home/artem/Downloads", "GET", httpParams, "");
		tstRequestHandler = testServer.new RequestHandler(tstRequest);
		tstRequestHandler.run();
		//reading handled page
	    InputStreamReader in = new InputStreamReader(socketClient.getInputStream());
	      int c;
	      try{
	      while ((c = in.read())!=-1)
	        System.out.print((char)c);
	      }catch (SocketException e){
	    	  if (!e.getMessage().equals("Connection reset"))
	    	  {
	    		  throw e;
	    	  }
	      }
	      
	}
	
	@Test
	public void testBasicRun2() throws IOException {
		//test for basic response, asking for html
		Map<String,String> httpParams = new HashMap<String, String>();
		httpParams.put("host", "127.0.0.1");
		PrintStream out = new PrintStream(socketClient.getOutputStream());
		out.write(0);
		out.flush();
		while (socketServer == null);
		tstRequest = new Request(socketServer, "/home/artem/eclipseJee/eclipse/about.html", "GET", httpParams, "");
		tstRequestHandler = testServer.new RequestHandler(tstRequest);
		tstRequestHandler.run();
		//reading handled page
	    InputStreamReader in = new InputStreamReader(socketClient.getInputStream());
	      int c;
	      try{
	      while ((c = in.read())!=-1)
	        System.out.print((char)c);
	      }catch (SocketException e){
	    	  if (!e.getMessage().equals("Connection reset"))
	    	  {
	    		  throw e;
	    	  }
	      }
	      
	}
	
	
	
}
